package mapping

import (
	"CSMS/DAO"
	"CSMS/funclib"
	"database/sql"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	"time"
)

//OperateCenterGET 返回操作中心页面
func OperateCenterGET(c *gin.Context) {
	userRank, _ := c.Cookie("userRank")
	ID, _ := c.Cookie("ID")
	user1 := DAO.User{}
	user1.SelectUserByNo(ID)
	//获取登录用户信息

	c.HTML(http.StatusOK, "users/operateCenter.html", gin.H{
		"UserName": user1.UserName.String,
		"userRank": userRank,
	})
	return
}

//orderGoodsGET 获取订货页面
func OrderGoodsGET(c *gin.Context) {
	//获取订货html
	c.HTML(http.StatusOK, "users/orderGoods.html", gin.H{

	})
}

//goodListGET 获取订货列表
func GoodListGET(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": "OK",
		"data": DAO.SelectGoodList(),
	})
}

//updateIndentPost 更新订单
func UpdateIndentPOST(c *gin.Context) {
	//获取基本数据
	ID, _ := c.Cookie("ID")
	userNo, _ := strconv.Atoi(ID)
	indentDate := time.Now().Format("2006-01-02")
	goodNoList := c.PostForm("goodNoList")
	requireNumList := c.PostForm("requireNumList")
	deliveryDateList := c.PostForm("deliveryDateList")
	fmt.Println(indentDate)
	fmt.Println(goodNoList)
	fmt.Println(requireNumList)
	fmt.Println(deliveryDateList)

	//将数据转换成对应格式
	goodNoListInt := funclib.StringListToInt(strings.Split(goodNoList, ","))
	requireNumListInt := funclib.StringListToInt(strings.Split(requireNumList, ","))
	deliveryDateListString := strings.Split(deliveryDateList, ",")

	//获取商品对象
	goods := DAO.SelectALLByNo(goodNoListInt)
	fmt.Println(goods)

	//计算商品总价格
	var IndentTotalPrice int
	for i := 0; i < len(goodNoListInt); i++ {
		IndentTotalPrice += goods[i].GoodPrice * requireNumListInt[i]
	}

	//计算商品折扣,以及折扣后价格
	priceDiscount := DAO.JudgeDiscount(IndentTotalPrice)
	realPrice := priceDiscount * float64(IndentTotalPrice)
	fmt.Println(priceDiscount)
	//写入订单
	ind := DAO.Indent{
		IndentNo:         0,
		UserNo:           userNo,
		IndentDate:       sql.NullString{String: indentDate, Valid: true},
		IndentTotalPrice: IndentTotalPrice,
		IndentDiscount:   priceDiscount,
		IndentRealPrice:  realPrice,
	}
	nowIndentNo := ind.InsertIndent()

	//更新订单-商品表
	for i := 0; i < len(goods); i++ {
		deli := DAO.Delivery{
			IndentNo:       nowIndentNo,
			GoodNo:         goods[i].GoodNo,
			GoodNum:        requireNumListInt[i],
			DeliveryData:   sql.NullString{String: deliveryDateListString[i], Valid: true},
			GoodTotalPrice: requireNumListInt[i] * goods[i].GoodPrice,
		}
		deli.InsertDelivery()
	}

	c.JSON(http.StatusOK, gin.H{
		"code": "IndentOK",
	})
	return
}

//QueryIndentGET 获取查询订单页面
func QueryIndentGET(c *gin.Context) {
	c.HTML(http.StatusOK, "users/queryIndent.html", gin.H{

	})
}

//IndentListGET 获取已查询订单列表
func IndentListGET(c *gin.Context) {
	//传入参数
	userNoPost, _ := strconv.Atoi(c.PostForm("userNo"))
	indentDatePost := c.PostForm("indentDate")
	//实际用户以及职称
	t, _ := c.Cookie("ID")
	userNoReal, _ := strconv.Atoi(t)
	userRankReal, _ := c.Cookie("userRank")
	fmt.Println(userNoPost, indentDatePost, userNoReal, userRankReal)
	//不同用户的查询所得到的数据结构不一样
	indentListData := DAO.SLByUserNoORIndentDate(userNoPost, indentDatePost)
	switch userRankReal {
	//负责人
	case "principal":
		consignment := DAO.Consignment{ConsignmentUserNo: userNoReal}
		consignment.SelectConsignmentByLoginId()
		//获取Indent列表信息
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"data": gin.H{
				"indentListData": IndentJSONBYRank(userNoReal, userRankReal, indentListData),
			},
		})
		return
	case "saleSupervisor":
		c.JSON(http.StatusOK, gin.H{
			"code": 1,
			"data": gin.H{
				"indentListData": IndentJSONBYRank(userNoReal, userRankReal, indentListData),
			},
		})
		return

	default:
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
		})
		return
	}
}

//IndentDetailGET 获取订单明细，显示订购商品相关信息
func IndentDetailGET(c *gin.Context) {
	//获取订单编号
	indentNo, _ := strconv.Atoi(c.PostForm("indentNo"))

	//返回订单对应商品信息
	var deliveryInfoList []DAO.DeliveryInfo
	deliveryInfoList = DAO.SelectDInfoByIndentNo(indentNo)

	//生成商品信息JSON
	var deliveryInfoJSON []gin.H
	for i := 0; i < len(deliveryInfoList); i++ {
		deliveryInfoJSON = append(deliveryInfoJSON, gin.H{
			"GoodNo":           deliveryInfoList[i].Good.GoodNo,
			"GoodName":         deliveryInfoList[i].Good.GoodName,
			"GoodPrice":        deliveryInfoList[i].Good.GoodPrice,
			"GoodNum":          deliveryInfoList[i].Delivery.GoodNum,
			"GoodDeliveryDate": deliveryInfoList[i].Delivery.DeliveryData,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"data": gin.H{
			"consignmentInfo":    DAO.SelectConsignmentInfo(indentNo),
			"deliveryDetailList": deliveryInfoJSON,
		},
	})
}

//DeliveryInventory 跳转送货清单页面
func DeliveryInventoryGET(c *gin.Context) {
	nowDate := time.Now()
	c.HTML(http.StatusOK, "users/deliveryInventory.html", gin.H{
		"NowDate": nowDate.Format("2006-01-02"),
	})
}

//TodayDeliveryListGET 生成送货清单
func TodayDeliveryListGET(c *gin.Context) {
	nowDate := c.PostForm("nowDate")
	userRank, _ := c.Cookie("userRank")
	//当用户不是送货主管时
	if userRank != "deliverySupervisor" {
		c.JSON(http.StatusBadRequest, gin.H{

		})
		return
	}
	todayDeliveryList := DAO.SelectTodayDelivery(nowDate)
	var todayDeliveryListJSON []gin.H
	for i := 0; i < len(todayDeliveryList); i++ {
		todayDeliveryListJSON = append(todayDeliveryListJSON, gin.H{
			"GoodNo":          todayDeliveryList[i].Good.GoodNo,
			"GoodName":        todayDeliveryList[i].Good.GoodName,
			"GoodNum":         todayDeliveryList[i].Delivery.GoodNum,
			"GoodTotalPrice":  todayDeliveryList[i].Delivery.GoodTotalPrice,
			"ConsignmentName": todayDeliveryList[i].Consignment.ConsignmentName,
			"ConsignmentAdr":  todayDeliveryList[i].Consignment.ConsignmentAdr,
			"ConsignmentTel":  todayDeliveryList[i].Consignment.ConsignmentTel,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"data": gin.H{
			"todayDeliveryList": todayDeliveryListJSON,
		},
	})
}

//FinanceInventoryGET 返回财务清单页面
func FinanceInventoryGET(c *gin.Context) {
	c.HTML(http.StatusOK, "users/financeInventory.html", gin.H{

	})
}

//DiscountListGET 返回折扣JSON信息
func DiscountListGET(c *gin.Context) {
	discountList := DAO.SelectDiscountList()
	var discountListJSON []gin.H
	for i := 0; i < len(discountList); i++ {
		discountListJSON = append(discountListJSON, gin.H{
			"MinMoney":     discountList[i].MinMoney,
			"MaxMoney":     discountList[i].MaxMoney,
			"DiscountRate": discountList[i].DiscountRate,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"data": gin.H{
			"discountList": discountListJSON,
		},
	})
	return
}

//FinanceInfo 返回财务清单信息
func FinanceInfoGET(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"data": gin.H{
			"financeInfo": DAO.SelectFinanceInfo(),
		},
	})
}

//NowMonthUserGET 返回每月用户清单表页面
func NowMonthUserGET(c *gin.Context) {
	c.HTML(http.StatusOK, "users/nowMonthUser.html", gin.H{

	})
}

//NowMonthUserInfoGET 返回当月用户清单信息
func NowMonthUserInfoGET(c *gin.Context) {
	nowDate := time.Now().Format("2006-01-02")
	nowDateYear := strings.Split(nowDate, "-")[0]
	nowDateMonth := strings.Split(nowDate, "-")[1]

	c.JSON(http.StatusOK, gin.H{
		"code": 1,
		"data": gin.H{
			"nowMonthUserList": DAO.SelectByNowMonth(nowDateYear, nowDateMonth),
		},
	})
}

//功能函数-------------
//indentJSONBYRank 通过用户rank返回不同的indentJSON序列
func IndentJSONBYRank(userNoReal int, userRank string, indentList []DAO.Indent) []gin.H {
	var indentJson []gin.H
	switch userRank {
	//负责人
	case "principal":
		for i := 0; i < len(indentList); i++ {
			if indentList[i].UserNo == userNoReal {
				indentJson = append(indentJson, gin.H{
					"IndentNo":         indentList[i].IndentNo,
					"IndentDate":       indentList[i].IndentDate,
					"IndentTotalPrice": indentList[i].IndentTotalPrice,
					"IndentDiscount":   indentList[i].IndentDiscount,
					"IndentRealPrice":  indentList[i].IndentRealPrice,
				})
			}
		}
		break
	case "saleSupervisor":
		for i := 0; i < len(indentList); i++ {
			indentJson = append(indentJson, gin.H{
				"IndentNo":         indentList[i].IndentNo,
				"IndentDate":       indentList[i].IndentDate,
				"IndentTotalPrice": indentList[i].IndentTotalPrice,
				"IndentDiscount":   indentList[i].IndentDiscount,
				"IndentRealPrice":  indentList[i].IndentRealPrice,
			})
		}
		break
	default:
		indentJson = nil
	}
	return indentJson
}
